Kubernetes完全ガイド 第2版
https://gyazo.com/5ff57a118f80b2ec04f41fe9728385dd
Kubernetes完全ガイド 第2版 (Top Gear) | ⻘⼭ 真也 |本 | 通販 | Amazon
MasayaAoyama/kubernetes-perfect-guide: 『Kubernetes完全ガイド』の付録マニフェストのリポジトリ / "Kubernetes perfect guide" sample manifest repository
CNCF
スケジューリング
どの Node に配置するか
Affinity / Anti-Affinity
セルフヒーリング
Node 障害時に別の Node にコンテナを振り分ける
etcd etcd | Home
Kubernetes 設定のデータストア
minikube
hyperlit
kind
kubernetes-sigs/kind: Kubernetes IN Docker - local clusters for testing Kubernetes
ローカルでマルチノードクラスタ作って試せる
brew install kind
kubernetes-perfect-guide/samples/chapter03 at 2nd-edition · MasayaAoyama/kubernetes-perfect-guide
FeatureGates
フィーチャーゲート | Kubernetes
beta 以上はデフォルトで有効、alpha は明示的にゆうこうにする
kubeadmのインストール | Kubernetes
Flannel
Pod 同士のネットワークを構成する
クラスターのネットワーク | Kubernetes
GKE
プリエンプティブルインスタンスを利用して費用を下げる
プリエンプティブル VM インスタンス  |  Compute Engine ドキュメント  |  Google Cloud
$ gcloud container get-server-config --zone asia-northeast1-a
NodePool
ノードにラベル付けしてグルーピングする
Playground
Play with Kubernetes
GKE 準備
必要になったらやる
BRAC
$ kubectl create clusterrolebinding user-cluster-admin-binding
権限の定義と割り当て - ロールベースのアクセス制御  |  Kubernetes Engine ドキュメント  |  Google Cloud
Workload Identity
ワークロード ID  |  Kubernetes Engine ドキュメント  |  Google Cloud
基礎
Master / Node
リソースのカテゴリ
Kubernetes API Reference Docs
Workloads
Service
Config & Storage
Cluster
Metadata
Namespace
クラスタを仮想的に分離
デフォルト kube-system, kube-public, kube-node-lease, default
チームや環境で分ける
* 環境は Namespace でなくクラスタで分ける
クラスタアップデート, マニフェストの再利用性, Service の名前解決
kubectl
複数のクラスターへのアクセスを設定する | Kubernetes
clusters / users / contexts
context = clusters + users + namespace(optional)
$ kubectl config
~/.kube/config への操作 set-coluster / set-credentials / set-context
get-contexts で一覧
use-context CONTEXT で切り替え
current-context で今の context
kubectl --context=CONTEXT でコマンドごとに指定
kubectx & kubens ahmetb/kubectx: Faster way to switch between clusters and namespaces in kubectl
$ brew install kubectx で入る
kind でちょっと立ててみる
GKE#5f72dcd838446100002ad56b
$ kubectl create -f sample-pod.yaml
$ kubectl get pods
$ kubectl delete -f sample-pod.yaml
$ kubectl delete pod sample-pod
$ kubectl delete-f sample-pod.yaml --wait
--wait で終了を待つ
$ kubectl apply -f sample-pod.yaml
前回適用したマニフェスト、現在のリソースの状態、今回適用するマニフェストから差分がでる
* 常に apply で適用するのがよい
Server-side apply
差分検知は手元との差なので衝突する
kubectl set image pod ... 等でマニフェスト経由せず直接編集されうる
* $ kubectl apply --server-side を使う
--force-conflicts
フィールドを管理しているコンポーネント = manager
kubectl get pod sample-pod -o yaml
--field-manager=
$ kubectl rollout restart deployment sample-deployment
$ kubectl wait --for=condition=Ready pod/sample-pod
$ kubectl wait --for=condition=PodScheduled pod --all
マニフェストファイルの設計
1つのマニフェストに複数のリソースを定義
実行順序を厳密にしたい場合、リソースの結合度が強い場合
--- で区切って複数書く
複数同時適用
kubectl apply -f ./dir, -R で再帰的
ディレクトリで適用, 細かくディレクトリ分割すると適用順序の制御は難しくなる
アノテーションとラベル
どちらも metadata 直下
アノテーション: システムコンポーネントが利用する
GKE, EKS 等環境特有の拡張機能
ラベル: リソースの管理に利用する
$ kubectl get pods -l label1=val1,label2
ラベルに対してレプリカ数を制御したり、ラベルに対してトラフィックを流したり
推奨ラベル(Recommended Labels) | Kubernetes
kubectl apply --prune で削除されたリソースを自動で処分
* --all すると渡していないマニフェストのリソースも全消えになるので、label と同時に使う
$ kubectl diff -f sample-pod.yaml で差分を見る
$ kubectl api-resources
Namespace レベルのリソースとクラスタレベルのリソース
$ kubectl api-resources --namespaced=true # Namespace
$ kubectl api-resources --namespaced=false # Cluster
リソースの確認
$ kubectl get pods
$ kubectl get pod NAME
$ kubectl get nodes
$ kubectl get all
--show-labels
--output=FORMAT
wide, json, jsonpath, いろいろ
custom-columns=GO_TEMPLATE
--watch, --output-watch-events
$ kubectl describe
pod sample-pod でイベントや状態、クラスタ情報など
node gke-k8s-default-pool-xxx で Node のリソース状況など
$ kubectl top node
$ kubectl top pod
$ kubectl exec -it sampe-pod -- /bin/ls
デバッグコンテナ
$ kubectl alpha debug sample-pod --image=... --container debug-container
$ kubectl exec -it sample-pod -c debug-container -- bash
ポートフォワーディング
$ kubectl port-forward sample-pod 8888:80
local:container
ログ
$ kubectl logs sample-pod
$ kubectl logs sample-pod -f
-f で follow
--since=1h, --timestamp=true --tail=10, --selector=env=prod
ファイルのコピー
$ kubectl cp sample-pod:etc/hostname ./hostname
プラグイン
$ kubectl plugin list
kubernetes-sigs/krew: 📦 Find and install kubectl plugins
Installing · Krew
verbose
-v=level
$ kubectl -v=6 apply -f sample-pod.yaml
-v=6 は API Request / Reponse
$ kubectl -v-8 apply -f sample-pod.yaml
-v=8 は API Request Body / Reponse Body レベル
k8s にまとめた
Pod < ReplicaSet < Deployment
Pod < Job < CronJob
spec.jobTemplate.spec.{completions, parallelism, backoffLimit} spec.suspend: true で一時的に実行を止めれる
spec.concurrencyPolicy は Allow | Forbid | Replace
Pod で複数コンテナ動かす
サイドカー: 機能追加, データの扱い系
アンバサダー: 外部との中継, Cloud SQL Proxy
アダプタ: 外部からのアクセスのインターフェース
ENTRYPOINT が command CMD が args
hostNetwork: true
基本 NodePort Service を使う、同じ Node 上で port 取り合いになる
dnsProxy
PodのDNSポリシー - ServiceとPodに対するDNS | Kubernetes
ReplicaSet
$ k describe rs
ラベル重複など管理がミスったときに削除されうる
セレクタ
等価ベース(Equality-based) の要件(requirement) - ラベル(Labels)とセレクター(Selectors) | Kubernetes
environment = production
集合ベース(Set-based) の要件(requirement) - ラベル(Labels)とセレクター(Selectors) | Kubernetes
environment in (production, qa)
Deployment
$ k rollout status deployment DEPLOYMENT
ローリングアップデートの際に通信待つとか Gracefull Shutdown になってるのかとか気になる
spec.template 以下の構造のハッシュを取って比較している
更新して戻したものを apply すると以前の ReplicaSet が再利用される
$ k rollout history deployment sample-deployment
$ k rollout undo deployment sample-deployment
spec.strategy.type
更新戦略 - Deployment | Kubernetes
maxSurge で余分に pod を作りつつ移行
DemonSet
Node ごとに1つPodを置く
ホスト単位でのリソース収集やログの収集など
StatefulSet
DB など
PersistentVolume
Job
終了を前提としたもの
並列数、成功回数
spec.ttlSecondAfterFinished
CronJob
suspend: true でメンテナンス等
spec.concurrencyPolicy 同時実行数の制御
spec.startingDeadlineSeconds 開始時刻が送れた場合に許容できる秒数